package com.cswangdao.realm;

import com.cswangdao.domain.model.MarketUser;
import com.cswangdao.mapper.MarketUserMapper;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
public class MyRealm extends AuthorizingRealm {

    @Autowired
    private MarketUserMapper userMapper;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String username = (String) authenticationToken.getPrincipal();
        MarketUser user = userMapper.selectByUsername(username);
        if(Objects.isNull(user)){
            throw new AuthenticationException("用户不存在！");
        }
        AuthenticationInfo info = new SimpleAuthenticationInfo(
                user.getId(),
                user.getPassword(),
                this.getName()
        );
        return info;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

}
